<template>
  <view>
    <!-- <view>{{refresh}}</view> -->
    <!-- <view>{{userinfoZ.yuangong_tel}}</view> -->
    <form @submit="formSubmitHt" bindreset="formReset">
      <view v-show="false">{{ refresh }}</view>
      <view class="set-box1">
        <text class="set-tex1">乙方姓名</text>
        <input
          type="text"
          class="set-inp"
          placeholder="输入您的姓名"
          name="yuangong_name"
          :value="userinfoZ.bdname"
          disabled
          @input="formModel('bdname', $event)"
        />
      </view>
      <view class="set-box1">
        <text class="set-tex1">身份证号码</text>
        <input
          type="text"
          class="set-inp"
          placeholder="输入您的身份证号"
          name="idcard"
          disabled
          @blur="getjsdw"
          :value="userinfoZ.bdsfz"
        />
      </view>

      <view class="set-box1">
        <text class="set-tex1">选择用工单位</text>
      </view>
      <view>
        <!-- radioChange -->
        <radio-group class="radio-group" @change="radioChangeZ">
          <label class="radio" v-for="(item, index) in items" :key="index">
            <view><radio :value="item.jsdwid" :checked="item.jsdwid == jsdwid" />{{ item.jsdwname }}</view>
          </label>
        </radio-group>
      </view>

      <view class="set-box1">
        <text class="set-tex1">电子邮箱</text>
        <input
          type="text"
          class="set-inp"
          placeholder="输入电子邮箱"
          :value="userinfoZ.yuangong_email"
          name="yuangong_email"
          @input="formModel('yuangong_email', $event)"
        />
      </view>

      <view class="set-box1">
        <text class="set-tex1">手机号码</text>
        <input
          type="text"
          class="set-inp"
          placeholder="输入手机号码"
          :value="userinfoZ.yuangong_tel"
          name="yuangong_tel"
          @input="formModel('yuangong_tel', $event)"
        />
      </view>

      <view class="set-box1">
        <text class="set-tex2">性别</text>
        <van-radio-group :value="radioz" @change="onChange">
          <view class="radio-group2">
            <van-radio name="男" checked-color="#07c160">男</van-radio>
            <van-radio name="女" checked-color="#07c160">女</van-radio>
          </view>
        </van-radio-group>
      </view>

      <view class="set-box1">
        <text class="set-tex1">紧密联系人</text>
        <input
          type="text"
          class="set-inp"
          :value="userinfoZ.jmlxr"
          placeholder="输入紧密联系人"
          name="jmlxr"
          @input="formModel('jmlxr', $event)"
        />
      </view>
      <view class="set-box1">
        <text class="set-tex1">紧密联系人电话</text>

        <input
          type="text"
          class="set-inp"
          placeholder="输入紧密联系人电话"
          name="jmlxrtel"
          :value="userinfoZ.jmlxrtel"
          @input="formModel('jmlxrtel', $event)"
        />
      </view>
      <view class="set-box1">
        <text class="set-tex1">住址</text>
        <input
          type="text"
          class="set-inp"
          placeholder="输入住址"
          name="yuangong_addr"
          :value="userinfoZ.yuangong_addr"
          @input="formModel('yuangong_addr', $event)"
        />
      </view>

      <button class="set-btn" formType="submit" :loading="loading" :disabled="showjs == 1 || disabled">
        提交劳动合同
      </button>
    </form>
  </view>
</template>

<script>
import { dzhtadd, ygdata } from '@api/zm';
import Taro from '@tarojs/taro';
import Toast from '@components/vant/toast/toast';
import Notify from '@components/vant/notify/notify';
export default {
  data() {
    return {
      radioz: '',
      yuangong_ssgsid: '',
      jsdwid: '',
      refresh: '',
      disabled: false,
      loading: false,
    };
  },
  computed: {
    userinfoZ() {
      return this.userinfo;
    },
  },
  props: {
    userinfo: {
      default: [],
      required: true,
    },
    items: {
      type: Array,
      default: [],
      required: true,
    },
    // jsdwid: {
    //   required: true,
    // },
    showjs: {
      type: Number,
      default: 1,
      required: true,
    },
    formModel: {
      type: Function,
      default: null,
      required: true,
    },
    formSubmit: {
      type: Function,
      default: null,
      required: true,
    },
    getjsdw: {
      type: Function,
      default: null,
      required: true,
    },
    radioChange: {
      type: Function,
      default: null,
      required: true,
    },
  },

  methods: {
    haha() {
      Notify('通知内容');
    },
    onChange(e) {
      this.radioz = e.detail;
    },

    radioChangeZ(e) {
      this.jsdwid = e.detail.value;
      let yuangong_ssgsid = this.items.find((item) => item.jsdwid == e.detail.value);
      this.yuangong_ssgsid = yuangong_ssgsid.yuangong_ssgsid;
      console.log(this.jsdwid, 'this.jsdwid');
      Taro.showLoading({
        title: '正在为你自动填充信息',
      });

      ygdata({
        formdata: { yuangong_name: this.userinfoZ.bdname, idcard: this.userinfoZ.bdsfz, jsdwid: this.jsdwid },
      }).then((res) => {
        let { yuangong_lxdz, yuangong_sj, yuangong_sex } = res.data;

        this.$set(this.userinfoZ, 'yuangong_addr', yuangong_lxdz);
        this.$set(this.userinfoZ, 'yuangong_tel', yuangong_sj);
        this.radioz = yuangong_sex == 1 ? '男' : '女';
        //  this.refresh=1
        console.log(this.userinfoZ.yuangong_addr, 'this.userinfoZ.yuangong_addr');
      });
    },
    formSubmitHt: function (e) {
      let formdata = e.detail.value;
      console.log(formdata, 'e');
      let errinfo = '';
      let { yuangong_tel, yuangong_email, jmlxrtel } = e.detail.value;

      if (!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(yuangong_tel)) {
        errinfo = errinfo.concat('手机号码', ' ', ' ');
      }
      if (!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(jmlxrtel)) {
        errinfo = errinfo.concat('紧密联系人手机号码', ' ', ' ');
      }
      if (!/^([a-zA-Z\d])(\w|\-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/.test(yuangong_email)) {
        errinfo = errinfo.concat('邮箱错误', ' ', ' ');
      }
      if (errinfo) {
        Notify(`${errinfo}格式错误,请重新输入`);
        return;
      }

      formdata.yuangong_sex = this.radioz;
      formdata.jsdwid = this.jsdwid;
      formdata.yuangong_ssgsid = this.yuangong_ssgsid;
      delete formdata['女'];
      delete formdata['男'];

      this.disabled = true;
      this.loading = true;
      dzhtadd({ formdata: formdata })
        .then((res) => {
          if (!res.code) {
            Toast({
              message: '提交成功!等待管理员审核',
              onClose: () => {
                // let { zmid, num } = res.data;
                // let type = this.type;
                Taro.reLaunch({
                  url: '/pages/contract/index',
                });
                // let youji = this.radioz;
                // this.show = true;
                // Taro.navigateTo({
                //   url: `/pages/checkout/index?id=${zmid}&zmtype=${type}&paynum=${num}&youji=3&type=1`,
                // });
              },
            });
          }
        }).catch(()=>{
           this.disabled = false;
        })
        .finally(() => {
          // this.disabled = false;
          this.loading = false;
        });
    },
  },
};
</script>

<style>
.errinof {
  color: red;
}
</style>